// Package tools @program: account
//@author: edte
//@create: 2021-12-27 21:19
package tools

import (
	log2 "account/log"
	"context"
	"crypto/md5"
	"errors"
	"fmt"
	"io"
)

// Md5 Md5加密
func Md5(str string) (string, error) {
	var err error = nil
	//str传入要加密的字符串
	h := md5.New()
	_, err = io.WriteString(h, str)
	if err != nil {
		writeLog, err := log2.WriteLog(context.Background(), "account", "Md5", "md5", "Error", "Md5 encryption failed")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err != nil {
			fmt.Println("写入日志失败")
		}
		return "", errors.New("md5加密失败")
	}
	return fmt.Sprintf("%x", h.Sum(nil)), err
}
